package com.zhaodui.loan.service.impl;
import com.zhaodui.loan.service.FxjLoanFkServiceI;
import com.zhaodui.util.fxjUtil;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.zhaodui.loan.entity.FxjLoanFkEntity;
import com.zhaodui.loan.page.FxjLoanFkPage;
import com.zhaodui.loan.entity.FxjLoanFkMeEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

import static com.zhaodui.util.ConStant.bpms80;
import static com.zhaodui.util.ConStant.bpms85;

@Service("fxjLoanFkService")
@Transactional
public class FxjLoanFkServiceImpl extends CommonServiceImpl implements FxjLoanFkServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
 	public void delete(FxjLoanFkEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(FxjLoanFkPage fxjLoanFkPage) throws Exception {
		FxjLoanFkEntity fxjLoanFk = new FxjLoanFkEntity();
		MyBeanUtils.copyBeanNotNull2Bean(fxjLoanFkPage, fxjLoanFk);
		//保存主信息
		this.save(fxjLoanFk);
		/**保存-放款明细*/
		List<FxjLoanFkMeEntity> fxjLoanFkMeList = fxjLoanFkPage.getFxjLoanFkMeList();
		for(FxjLoanFkMeEntity fxjLoanFkMe:fxjLoanFkMeList){
			//外键设置
			fxjLoanFkMe.setOrderNo(fxjLoanFk.getOrderNo());
			this.save(fxjLoanFkMe);
		}
	}

	
	public void updateMain(FxjLoanFkPage fxjLoanFkPage) throws Exception{
		FxjLoanFkEntity fxjLoanFk = new FxjLoanFkEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(fxjLoanFkPage.getId())){
			fxjLoanFk = findUniqueByProperty(FxjLoanFkEntity.class, "id", fxjLoanFkPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(fxjLoanFkPage, fxjLoanFk);
			this.saveOrUpdate(fxjLoanFk);
		}else{
			this.saveOrUpdate(fxjLoanFk);
		}
			fxjUtil.genRePlan(fxjLoanFk.getOrderNo());


		//===================================================================================
		//获取参数
		Object orderNo0 = fxjLoanFk.getOrderNo();
		//===================================================================================
		//1.放款明细数据库的数据
	    String hql0 = "from FxjLoanFkMeEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjLoanFkMeEntity> fxjLoanFkMeOldList = this.findHql(hql0,orderNo0);
		//2.放款明细新的数据
		List<FxjLoanFkMeEntity> fxjLoanFkMeList = fxjLoanFkPage.getFxjLoanFkMeList();
	    //3.筛选更新明细数据-放款明细
		if(fxjLoanFkMeList!=null &&fxjLoanFkMeList.size()>0){
			for(FxjLoanFkMeEntity oldE:fxjLoanFkMeOldList){
				boolean isUpdate = false;
				for(FxjLoanFkMeEntity sendE:fxjLoanFkMeList){
					//需要更新的明细数据-放款明细
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-放款明细
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-放款明细
			for(FxjLoanFkMeEntity fxjLoanFkMe:fxjLoanFkMeList){
				if(oConvertUtils.isEmpty(fxjLoanFkMe.getId())){
					//外键设置
					fxjLoanFkMe.setOrderNo(fxjLoanFk.getOrderNo());
					this.save(fxjLoanFkMe);
				}
			}
		}
	}

	
	public void delMain(FxjLoanFkEntity fxjLoanFk) throws Exception{
		//删除主表信息
		this.delete(fxjLoanFk);
		//===================================================================================
		//获取参数
		Object orderNo0 = fxjLoanFk.getOrderNo();
		//===================================================================================
		//删除-放款明细
	    String hql0 = "from FxjLoanFkMeEntity where 1 = 1 AND orderNo = ? ";
	    List<FxjLoanFkMeEntity> fxjLoanFkMeOldList = this.findHql(hql0,orderNo0);
		this.deleteAllEntitie(fxjLoanFkMeOldList);
	}
	
 	
}